package org.minuframework.lang;

import java.util.List;

import org.minuframework.json.GsonUtils;

import com.google.common.collect.Lists;

public class ListUtils {
    
    /**
     * 合并集合
     * @param  list  集合
     * @param  list2 集合
     * @return       集合
     */
    public static <T> List<T> addAll(List<T> list, List<T> list2) {
        List<T> paramList = Lists.newArrayList(list);
        if (list2.isEmpty()) { return paramList; }
        paramList.addAll(list2);
        return paramList;
    }
    
    /**
     * 合并集合（不重复的）
     * @param  list  集合
     * @param  list2 集合
     * @return       集合
     */
    public static <T> List<T> addAllNoRepeat(List<T> list, List<T> list2) {
        List<T> paramList = Lists.newArrayList(list);
        if (list2.isEmpty()) { return paramList; }
        for (T t : list2) {
            if (!paramList.contains(t)) {
                paramList.add(t);
            }
        }
        return paramList;
    }
    
    public static byte[] asByteArray(List<Byte> list) {
        byte[] result = new byte[]{};
        if (list == null || list.isEmpty()) { return result; }
        result = new byte[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
    
    public static short[] asShortArray(List<Short> list) {
        short[] result = new short[]{};
        if (list == null || list.isEmpty()) { return result; }
        result = new short[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
    
    public static int[] asIntegerArray(List<Integer> list) {
        int[] result = new int[]{};
        if (list == null || list.isEmpty()) { return result; }
        result = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
    
    public static long[] asLongArray(List<Long> list) {
        long[] result = new long[]{};
        if (list == null || list.isEmpty()) { return result; }
        result = new long[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
    
    public static float[] asFloatArray(List<Float> list) {
        float[] result = new float[]{};
        if (list == null || list.isEmpty()) { return result; }
        result = new float[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
    
    public static double[] asDoubleArray(List<Double> list) {
        double[] result = new double[]{};
        if (list == null || list.isEmpty()) { return result; }
        result = new double[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
    
    public static char[] asCharacterArray(List<Character> list) {
        char[] result = new char[]{};
        if (list == null || list.isEmpty()) { return result; }
        result = new char[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
    
    public static boolean[] asBooleanArray(List<Boolean> list) {
        boolean[] result = new boolean[]{};
        if (list == null || list.isEmpty()) { return result; }
        result = new boolean[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
    
    public static String[] asStringArray(List<String> list) {
        String[] result = new String[]{};
        if (list == null || list.isEmpty()) { return result; }
        result = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
    
    public static Object[] asArray(List<Object> list) {
        Object[] result = new Object[]{};
        if (list == null || list.isEmpty()) { return result; }
        result = new Object[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
    
    public static void main(String[] args) {
        List<String> list = Lists.newArrayList();
        list.add("mi1");
        list.add("mi2");
        list.add("mi3");
        list.add("mi4");
        String[] strings = asStringArray(list);
        System.out.println(GsonUtils.toGSONString(strings));
    }
    
}
